package com.ranchuanyin.schoolcat.controller;

import cn.hutool.core.bean.BeanUtil;
import com.ranchuanyin.schoolcat.domain.CatAccount;
import com.ranchuanyin.schoolcat.dto.AccountDetailDTO;
import com.ranchuanyin.schoolcat.dto.AccountIdDTO;
import com.ranchuanyin.schoolcat.service.AccountDetailService;
import com.ranchuanyin.schoolcat.service.CatAccountService;
import com.ranchuanyin.schoolcat.service.CatGroupService;
import com.ranchuanyin.schoolcat.units.RestBean;
import com.ranchuanyin.schoolcat.vo.CatAccountDetailVO;
import com.ranchuanyin.schoolcat.vo.CatAccountVO;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Validated
@RequestMapping("/cat/user")
public class CatAccountController {
    @Resource
    AccountDetailService accountDetailService;
    @Resource
    CatAccountService catAccountService;

    @Resource
    CatGroupService catGroupService;

    @PostMapping("/detail")
    public RestBean<CatAccountDetailVO> getUserDetail(@Validated @RequestBody AccountIdDTO dto) {
        CatAccountDetailVO userDetail = accountDetailService.getUserDetail(dto);
        return RestBean.success(userDetail);
    }

    @PostMapping("/update")
    public RestBean<String> updateUserDetail(@RequestBody AccountDetailDTO detail) {

        Integer i = accountDetailService.updateUserDetail(detail);
        if (i == 0) {
            return RestBean.failure(200, "修改失败！");
        } else
            return RestBean.success("修改成功！");
    }

    @GetMapping("/{id}")
    public RestBean<Object> getUser(@PathVariable String id) {
        CatAccount byId = catAccountService.getById(id);
        Object bean = BeanUtil.toBean(byId, CatAccountVO.class);
        if (bean == null) {
            bean = catGroupService.getById(id);
        }
        return RestBean.success(bean);
    }
}
